import Vue from 'vue'
import VueRouter from 'vue-router'



//解决路由跳转原路由或者刷新出错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  //路由
  {
    path: '/',
    redirect: "/home"
  },
  //首页路由
  {
    path: '/home',
    name: 'home',
    component: () => import(/* webpackChunkName: "home" */ '../views/HomeView.vue'),
    meta: { 
       ShowTabber:true,
       num:0
    },
    children:[
      {
        path:"/home/popup",
        name:"popup",
        component:() => import(/* */ '../views/search.vue')
      }
    ]
  },
  //专题路由
  {
    path:"/topic",
    name:"topic",
    meta: { 
      ShowTabber:true,
      num:1
    },
    component: () => import(/* webpackChunkName: "topic" */ '../views/Topic.vue'),
  },
  //分类路由
  {
    path:"/classify",
    name:"classify",
    component: () => import(/* webpackChunkName: "classify" */ '../views/Classify.vue'),
    meta: { 
      ShowTabber:true,
      num:2
    },
    children:[
      {
        path:"/classify/popup",
        name:"clpopup",
        component:() => import(/* */ '../views/search.vue')
      }
    ]
  },
  //购物车路由
  {
    path:"/cart",
    name:"cart",
    meta: { 
      ShowTabber:true,
      num:3
    },
    component: () => import(/* webpackChunkName: "crat" */ '../views/Cart.vue'),
  },
  //我的路由
  {
    path:"/user",
    name:"user",
    meta: { 
      ShowTabber:true,
      num:4
   },
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue'),
  },
  // 登录路由
  { 
      path:"/login",
      name:"login",
      component: () => import(/* webpackChunkName: "login" */ '../views/login.vue'),
  },
  //品牌制作路由
  {
    path:"/branck",
    name:"branck",
    component: () => import(/* webpackChunkName: "branck" */ '../views/Branck.vue'),
  },
  //分类商品列表路由
  {
    path:"/channel",
    name:"channel",
    component: () => import(/* webpackChunkName: "channel" */ '../views/Channel.vue'),
  },
  //商品的详细路由
  {
    path:"/details",
    name:"details",
    component: () => import(/* webpackChunkName: "details" */ '../views/Details.vue'),
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由守卫 :
 router.beforeEach((to,from,next)=>{
   if(to.path == "/cart"){
     let token=window.sessionStorage.getItem("token");
    //  console.log(token);
      if(token){
        next()
      }else{
        next("/user")
      }
   }else{
    next()
   }
 })
export default router
